import QtQuick
import QtQuick.Controls
import EcgModel 1.0

Item {
    id:beatsGrid
    anchors.fill: parent

    signal rPeaksInAllChenged(int reaksInAll) // 定义一个信号
    function setCurrentIndex(index = 0){
        //设置高亮元素，并取消其他元素的选中状态
        longGridView.currentIndex = index;
        var rPeaksInAll =gridViewBeatsList.data(gridViewBeatsList.index(index,0),gridViewBeatsList.getRPeaksInAllRole());
        var arr = beatsModel.setSelectedBeats([rPeaksInAll]) //返回所有之前选中的心搏
        arr.push(rPeaksInAll)
        fragmentBeatsList.refreshMData(arr)
        gridViewBeatsList.refreshMData(arr)
        beatsGrid.rPeaksInAllChenged(rPeaksInAll)
    }
    GridView {
        id: longGridView
        anchors.fill: parent
        model: gridViewBeatsList
        cellWidth: longGridView.width > 1200?(longGridView.width-scrollBar.width)/ 6: (longGridView.width-scrollBar.width) /4
        cellHeight: longGridView.width > 1200?150:130
        highlight: Rectangle{color: "lightBlue"}
        focus:true
        clip:true
        highlightMoveDuration :0
        // currentIndex:2
        delegate: contactsDelegate

        Component {
            id: contactsDelegate
            Rectangle {
                id: wrapper
                width: longGridView.cellWidth - 5
                height: longGridView.cellHeight - 5
                border.color : selected ? "#ee6600" : "white"
                color:  selected ?"#fff7f1":"white"
                // opacity: 0.2
                // color:"#000000"
                radius: 2
                EcgCanvas {
                    start: rPeaksInAll - 1*EcgModel.sampleRate
                    duration: 2*1000
                    displayOneLead:true
                    canvasGain:0.6
                }
                Text {
                    text: label
                    color:"#666666"
                    padding:5
                }
                Text {
                    text: (rrIntervals || "-" )+ "ms"
                    color:"#666666"
                    padding:5
                    anchors.bottom: parent.bottom
                    anchors.left: parent.left
                }
                Text {
                    text: (rrIntervals ? Math.round(60 * 1000 / rrIntervals): "-") + "bpm"
                    color:"#666666"
                    padding:5
                    anchors.bottom: parent.bottom
                    anchors.right: parent.right
                }
                MouseArea {
                    anchors.fill: parent  // 使MouseArea覆盖整个Rectangle
                    onClicked: {
                        // 点击事件的处理逻辑
                        // console.log(index,value)
                        // 可以在这里添加更多的逻辑，比如改变Rectangle的颜色
                        longGridView.currentIndex = index
                    }
                }
            }
        }
        // 启用缓存以提高性能
        cacheBuffer: 400
        // 添加垂直滚动条
        ScrollBar.vertical: ScrollBar {
            id: scrollBar
            policy: ScrollBar.AlwaysOn
            width: 14
            minimumSize:0.06
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.bottom: parent.bottom
        }
        // onFocusChanged: function(){
        //     longGridView.focus = true
        // }

        Keys.onPressed:function(event) {
            let keyChar = event.text
            if(keyChar.length === 1 && keyChar.match(/[a-zA-Z]/)){
                let upperCaseChar = keyChar.toUpperCase()
                //修改心搏类型
                let arr = beatsModel.shortcutSetLabel(upperCaseChar);
                if(arr.length > 0){
                    fragmentBeatsList.refreshMData(arr)
                    gridViewBeatsList.refreshMData(arr)
                }
            }

            // if(event.key===Qt.Key_Up){
            //     longGridView.moveCurrentIndexUp()
            // }
            // else if(event.key===Qt.Key_Left){
            //     longGridView.moveCurrentIndexLeft()
            // }
            // else if(event.key===Qt.Key_Down){
            //     longGridView.moveCurrentIndexDown()
            // }
            // else if(event.key===Qt.Key_Right){
            //     longGridView.moveCurrentIndexRight()
            // }
        }
        onCurrentIndexChanged: {
            // console.log("Current index changed to:", currentIndex);
            var rPeaksInAll =gridViewBeatsList.data(gridViewBeatsList.index(currentIndex,0),gridViewBeatsList.getRPeaksInAllRole());
            // console.log(rPeaksInAll)
            var arr = beatsModel.setSelectedBeats([rPeaksInAll]) //返回所有之前选中的心搏
            arr.push(rPeaksInAll)
            // console.log(count)
            fragmentBeatsList.refreshMData(arr)
            gridViewBeatsList.refreshMData(arr)

            beatsGrid.rPeaksInAllChenged(rPeaksInAll)
        }
    }
    MouseArea {
        id: selectionArea
        // cursorShape:"CrossCursor"
        // anchors.fill: parent
        width: parent.width - scrollBar.width
        height: parent.height
        anchors.left: parent.left
        property real startX: 0
        property real startY: 0
        property bool selecting: false

        onPressed: function(mouse)  {
            startX = mouse.x
            startY = mouse.y
            selecting = true
            selectionRect.visible = true
            selectionRect.x = startX
            selectionRect.y = startY
            selectionRect.width = 0
            selectionRect.height = 0

            checkRPeaksInAlls(startX, startY, 1, 1)
        }

        onPositionChanged:function (mouse) {
            if (selecting) {
                let mouseX = mouse.x
                let mouseY = mouse.y
                if(mouseX < 0) mouseX = 0
                if(mouseY < 0) mouseY = 0
                if(mouseX > selectionArea.width) mouseX = selectionArea.width
                if(mouseY > selectionArea.height) mouseY = selectionArea.height
                var width = mouseX - startX
                var height = mouseY - startY
                selectionRect.x = width < 0 ? startX + width : startX
                selectionRect.y = height < 0 ? startY + height : startY
                selectionRect.width = Math.abs(width)
                selectionRect.height = Math.abs(height)

                checkRPeaksInAlls(selectionRect.x, selectionRect.y,  selectionRect.width, selectionRect.height)
            }
        }
        function rectIntersects(rect1, rect2) {
            return rect1.x < rect2.x + rect2.width &&
                    rect1.x + rect1.width > rect2.x &&
                    rect1.y < rect2.y + rect2.height &&
                    rect1.y + rect1.height > rect2.y
        }
        function checkRPeaksInAlls(startX, startY, width, height){
            //识别框住的心搏
            var rect = Qt.rect(startX, startY +longGridView.contentHeight*longGridView.ScrollBar.vertical.position, width, height)
            var rPeaksInAlls = []
            var index = null
            for (var i = 0; i < longGridView.contentItem.children.length; i++) {
                var item = longGridView.contentItem.children[i]
                var itemRect = Qt.rect(item.x, item.y, item.width, item.height)
                if (rectIntersects(rect, itemRect)) {
                    index = longGridView.indexAt(item.x, item.y)
                    var rPeaksInAll =gridViewBeatsList.data(gridViewBeatsList.index(index,0),gridViewBeatsList.getRPeaksInAllRole());
                    rPeaksInAlls.push(rPeaksInAll)
                }
            }
            if(index!== null){
                longGridView.currentIndex = index;
            }
            if(rPeaksInAlls.length > 0){
                var arr = beatsModel.setSelectedBeats(rPeaksInAlls) //返回所有之前选中的心搏
                arr.push(...rPeaksInAlls)
                // console.log(count)
                fragmentBeatsList.refreshMData(arr)
                gridViewBeatsList.refreshMData(arr)

                beatsGrid.rPeaksInAllChenged(rPeaksInAlls[rPeaksInAlls.length - 1])
            }
        }

        onReleased: function (mouse){
            selecting = false
            selectionRect.visible = false
        }
    }

    Rectangle {
        id: selectionRect
        border.color : "#1e90ff"
        color:  Qt.rgba(30/255, 144/255, 255/255, 60/255)
        border.width: 1
        visible: false
    }
    Component.onCompleted: {
        // gridViewBeatsList.getFragmentBeats(0,0);
        longGridView.currentIndex = 0; // 设置初始高亮项
    }
}
